/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-10-08 13:43:44
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-16 17:59:20
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { StoreFunctionsServiceProxy, StorePutServiceProxy, PartsDeliveryServiceProxy, StoreInfoServiceProxy, InventoryInput } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { InventoryComponent } from './inventory/inventory.component';
import { InventoryRecordComponent } from './inventory-record/inventory-record.component';
@Component({
    selector: 'app-warehouse-inventory',
    templateUrl: './warehouse-inventory.component.html',
    styleUrls: ['./warehouse-inventory.component.less']
})
export class WarehouseInventoryComponent extends AppComponentBase {
    @ViewChild('InventoryComponent') InventoryComponent: InventoryComponent;
    @ViewChild('InventoryRecordComponent') InventoryRecordComponent: InventoryRecordComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    stroeName = undefined;
    stroeNameOptions = [];
    materialsNode = undefined;
    materialsNodeOptions = [];
    rangeDates = undefined;
    status = undefined;
    person = undefined;
    personOptions = [];
    addFlag = false;
    actived = false;
    form = { no: undefined, stroeName: undefined, materialsNode: undefined, rangeDates: undefined };
    //日期显示中文
    chinese = {
        /** 每周第一天，0代表周日 */
        firstDayOfWeek: 0,
        /** 每周天数正常样式 */
        dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        /** 每周天数短样式（位置较小时显示） */
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        /** 每周天数最小样式 */
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        /** 每月月份正常样式 */
        monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        /** 每月月份短样式 */
        monthNamesShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
    };
    statusOptions = [
        {
            label: '已创建',
            value: 0
        }, {
            label: '盘点中',
            value: 1
        }, {
            label: '已盘点',
            value: 2
        }
    ];
    rowdata: any;
    title = undefined;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
        private _storeInfoServiceProxy: StoreInfoServiceProxy,
        private _partsDeliveryServiceProxy: PartsDeliveryServiceProxy,
    ) {
        super(injector);
    }
    end(row) {
        if (this.saveFlag) {
            this.saveFlag = false;
            this.message.confirm(
                '已盘点完成,确认结束盘点！',
                (isConfirmed) => {
                    if (isConfirmed) {
                        this._partsDeliveryServiceProxy
                            .finishInventory(row.id)
                            .subscribe((rs) => {
                                this.saveFlag = true;
                                if (rs.code == 0) {
                                    this.getTableList();
                                    this._messageService.success('操作成功');
                                } else {
                                    this._messageService.error(rs.message);
                                }
                            });
                    }
                }
            );
        }
    }
    addSave() {
        let input = new InventoryInput();
        input.inventoryNo = this.form.no;
        input.materialsNodeId = this.form.materialsNode;
        input.storeId = this.form.stroeName;
        for (const item of this.stroeNameOptions) {
            if (this.form.stroeName == item.value) {
                input.stroeName = item.label;
            }
        }
        input.inventoryStartTime = this.form.rangeDates[0];
        input.inventoryEndTime = this.form.rangeDates[1];
        if (this.saveFlag) {
            this.saveFlag = false;
            this._partsDeliveryServiceProxy.createInventoryStore(input)
                .pipe(finalize(() => { this.actived = false; }))
                .subscribe((rs) => {
                    this.saveFlag = true;
                    if (rs.code == 0) {
                        this._messageService.success('操作成功');
                        this.addFlag = false;
                        this.onHide(1);
                        this.getTableList();
                    } else {
                        this._messageService.error(rs.message);
                    }
                });
        }
    }
    onHide(event) {
        //新增盘点单关闭
        this.actived = false;
        this.form.materialsNode = undefined;
        this.form.no = undefined;
        this.form.rangeDates = undefined;
        this.form.stroeName = undefined;
    }
    reset() {
        this.stroeName = undefined;
    }
    lookResume(row) {
        this.rowdata = row;
        setTimeout(() => {
            this.InventoryRecordComponent.show();
            this.InventoryRecordComponent.getTableList();
        }, 10);
    }
    watch(row) {
        this.rowdata = row;
        this.title = '盘点查看';
        setTimeout(() => {
            this.InventoryComponent.show();
        }, 10);
    }
    update(row) {
        this.rowdata = row;
        this.title = '盘点';
        setTimeout(() => {
            this.InventoryComponent.show();
        }, 10);
    }
    add() {
        this.actived = true;
        this.addFlag = true;
        this._partsDeliveryServiceProxy.getInventoryNo(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.form.no = result;
            });

    }
    ngOnInit() {
        //仓库名称
        this._storeFunctionsServiceProxy.getStoreDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.stroeNameOptions = [];
                this.stroeNameOptions = result;
            });
        //物料类别
        this._storeFunctionsServiceProxy.getMaterialsTypeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialsNodeOptions = [];
                this.materialsNodeOptions = result;
            });
        //盘点人
        this._partsDeliveryServiceProxy.getInventoryPeopleList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.personOptions = [];
                this.personOptions = result;
            });

    }

    resert() {
        this.stroeName = undefined;
        this.materialsNode = undefined;
        this.rangeDates = undefined;
        this.status = undefined;
        this.person = undefined;
    }
    enum(value) {
        switch (value) {
            case 0:
                return '正常品';
            case 1:
                return '隔离品';
            case 2:
                return '返修品';
            case 3:
                return '报废品';
            case 4:
                return '返修正常品';
            default:
                return '';
        }
    }
    getTableList(event?: LazyLoadEvent) {
        let startTime = undefined;
        let endTime = undefined;
        if (this.rangeDates != undefined) {
            // startTime = moment(this.rangeDates[0]).format('YYYY-MM-DD ');
            // endTime = moment(this.rangeDates[1]).format('YYYY-MM-DD ');
            startTime = this.rangeDates[0];
            endTime = this.rangeDates[1];
        }
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._partsDeliveryServiceProxy.getInventorys(
            undefined,
            this.stroeName,
            startTime,
            endTime,
            this.status,
            this.person,
            this.materialsNode,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

}
